package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.BussTypeService;
import com.baichang.taoshunda.service.CarouselService;
import com.baichang.taoshunda.service.ScoreGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2017/8/18 0018.
 */
@Controller
@RequestMapping(ControllerConstants.HOMEPAGE)
public class HomePagecarouselMCtronller extends BaseMobileController {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private BussTypeService bussTypeService;
    @Autowired
    private BusinessService bussinessService;
    @Autowired
    private ScoreGoodsService scoreGoodsService;

    /**
     * 首页根据城市编号获取商家分类
     */

    @RequestMapping("/getHomePageType")
    public void getHomePageType(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer cityId = getInt(json, "cityId", 0);
                return bussTypeService.getHomePageType(cityId);
            }
        });
    }

    /**
     * 首页根据城市编号获取轮播图
     */
    @RequestMapping("/getCarouselByCity")
    public void getCarouselByCity(final HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer cityId = getInt(json, "cityId", 0);
                Integer type = getInt(json, "type", 0);  // 1首页轮播图  2.红包轮播图
                String lat = getString(json, "lat", "");
                String lng = getString(json, "lng", "");
                return carouselService.getCarouselByCity(cityId, type, lat, lng);
            }
        });
    }

    /**
     * 首页根据城市编号获取商家推荐
     */
    @RequestMapping("/getRecommendBussiness")
    public void getRecommendBussiness(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer cityId = getInt(json, "cityId", 0);//传的是区域id,接受的用的cityId
                String lat = getString(json, "lat", "");
                String lng = getString(json, "lng", "");
                return bussinessService.getRecommendBussiness(cityId, lat, lng);
            }
        });
    }

    /**
     * 获取所有的积分商品
     *
     * @return
     */
    @RequestMapping("/getScoreGoodsList")
    public void getScoreGoodsList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer nowPage = getInt(json, "nowPage", 1);
                Integer userId = getInt(json, "userId", 1);
                Integer pageSize = getInt(json, "pageSize", 20);
                return scoreGoodsService.getScoreGoodsList(nowPage, pageSize, userId);
            }
        });
    }

}
